/**
 * 
 */
package com.ordoict.smallsettlement.thread.task;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ordoict.smallsettlement.business.authentication.entity.AuthenticationEntity;
import com.ordoict.smallsettlement.business.authentication.service.AuthenticationService;
import com.ordoict.smallsettlement.common.splitter.FixedStringSplitter;

/**
 * DESC :
 * 
 * @company : OrdoICT.,Inc
 * @author : Choi Jin Wook // choijinwook84@ordoict.com
 * @version : 1.0.0
 * @since : 2013. 12. 4.
 * 
 */
@Component
@Scope("prototype")
public class TaskThread implements Runnable{

    @SuppressWarnings("unused")
    private static Logger logger = Logger.getLogger(TaskThread.class);

    @Autowired
    public TaskThread(ServerSocket serverSocket, String encodingSet) throws IOException {
        this(serverSocket);
        
        if(encodingSet != null && !encodingSet.equals("")){
            this.encodingSet = encodingSet;
            bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream(), encodingSet));
            printStream = new PrintStream(socket.getOutputStream(), true, encodingSet);
        }else{
            bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            printStream = new PrintStream(socket.getOutputStream());                
        }
    }
    
    public TaskThread(ServerSocket serverSocket) throws IOException {
        this.socket = serverSocket.accept();
    }
    
    public TaskThread() {
    }
    
    @Autowired
    private AuthenticationService service;
    private Socket socket;
    private BufferedReader bufferedReader;
    private PrintStream printStream;
    private String encodingSet;
    
    /**
     * @since 2013. 12. 10.
     * @return the encodingSet
     */
    public String getEncodingSet() {
        return encodingSet;
    }

    /**
     * @since 2013. 12. 10.
     * @param encodingSet the encodingSet to set
     */
    public void setEncodingSet(String encodingSet) {
        this.encodingSet = encodingSet;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public void run() {
        try {
            char[] charBuff = new char[500];
            int sts = 0;
            while (sts > -1) {
                    sts = bufferedReader.read(charBuff);
                    String n = String.valueOf(charBuff);
                    
                    System.out.println( "Received " + n.trim() + " from Connection " + socket.getInetAddress().getHostAddress().trim() + "." );
//                    AuthenticationEntity entity = new AuthenticationEntity();
                    
                    AuthenticationEntity entity = (AuthenticationEntity)FixedStringSplitter.split("AuthenticationEntity", n);
                    System.out.println(entity); 
                    entity = service.get(entity);
                    if(entity != null){
                        System.out.println( entity.getCpNm());
                        printStream.print( entity.getCpNm());
                    }else{
                        System.out.println("Data Not found!!!!!");
                        printStream.println("Data Not found!!!!!");
                    }
                    printStream.println("readLine:" + n.trim() ); 
                    charBuff = new char[50];
            }

            System.out.println( "Connection " + socket.getInetAddress().getHostAddress().trim() + " closed." );
            bufferedReader.close();
            printStream.close();
            socket.close();

        } catch (Exception e) {
            System.out.println(e);
        }
    }

}